package com.yunji.web.system;

import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.model.domain.SysConfigDO;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.system.context.SystemContext;
import com.yunji.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "参数配置信息操作处理")
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {
    @Autowired
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @ApiOperation("获取参数配置列表")
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public Response<List<SysConfigDO>> list(SysConfigDO config) {
        List<SysConfigDO> list = configService.selectConfigList(config);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出参数配置列表")
    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfigDO config) {
        List<SysConfigDO> list = configService.selectConfigList(config);
        ExcelUtil<SysConfigDO> util = new ExcelUtil<>(SysConfigDO.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @ApiOperation("分局参数编号获取详细信息")
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public Response<SysConfigDO> getInfo(@PathVariable Long configId) {
        return ResponseHelper.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @ApiOperation("分局参数键名查询参数值")
    @GetMapping(value = "/configKey/{configKey}")
    public Response<String> getConfigKey(@PathVariable String configKey) {
        return ResponseHelper.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @ApiOperation("新增参数配置")
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Response<Long> add(@Validated @RequestBody SysConfigDO config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return ResponseHelper.failure("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SystemContext.getUsername());
        Assert.isTrue(configService.insertConfig(config) > 0, "新增失败");
        return ResponseHelper.success(config.getConfigId());
    }

    /**
     * 修改参数配置
     */
    @ApiOperation("修改参数配置")
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response<String> edit(@Validated @RequestBody SysConfigDO config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return ResponseHelper.failure("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SystemContext.getUsername());
        Assert.isTrue(configService.updateConfig(config) > 0, "修改失败");
        return ResponseHelper.success();
    }

    /**
     * 删除参数配置
     */
    @ApiOperation("删除参数配置")
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public Response<String> remove(@PathVariable Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return ResponseHelper.success();
    }

    /**
     * 刷新参数缓存
     */
    @ApiOperation("刷新参数缓存")
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public Response<String> refreshCache() {
        configService.resetConfigCache();
        return ResponseHelper.success();
    }
}
